﻿using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 数据配置
    /// </summary>
    [MenuFilter]
    public class CodeController : AdminController
    {
        CodeService service = null;
        public CodeController()
        {
            service = new CodeService();
        }
        //首页
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetList()
        {
            LayTableModel result = new LayTableModel();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string type = Request.Params["codeType"];
                if (string.IsNullOrWhiteSpace(type))
                {
                    result.code = -1;
                    result.msg = "请选择类型";
                }
                else
                {
                    //参数
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    string[] par = { "CodeType", "CodeText", "CodeValue", "CodeRemark", "IsEnable", "IsDefault" };

                    foreach (string item in par)
                    {
                        if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                        {
                            dic.Add(item, Request.Params[item]);
                        }
                    }
                    int count = 0;

                    var list = service.GetCodeList(dic, out count);

                    result.data = list;
                    result.count = count;
                }

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        //添加或者修改视图
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0)
        {
            string CodeType = Request.Params["CodeType"];
            int ParentId = string.IsNullOrEmpty(Request.Params["ParentId"])
                ? 0 : int.Parse(Request.Params["ParentId"]);

            CodeOutput model = null;
            if (Id > 0)
            {
                model = service.Get<CodeOutput>(Id);
                CodeType = model.CodeType;
                ParentId = model.ParentId;
                ViewBag.CodeType = model.CodeType;
            }
            else
            {
                model = new CodeOutput();
                ViewBag.CodeType = CodeType;
            }

            //获取数据源
            IEnumerable<dynamic> CodeType_List = service.GetSelectListItem(CodeType, true, ParentId);

            //新建一个SelectListItem的列表
            List<SelectListItem> CodeTselectItemList = new List<SelectListItem>();

            //自定义编辑
            foreach (var item in CodeType_List)
            {
                CodeTselectItemList.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.CodeText, Selected = (item.Id == ParentId) });
            }

            //数据传入视图
            ViewBag.CodeList = CodeTselectItemList;

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [MenuRoute("Index")]
        public JsonResult Add(CodeInput model)
        {
            ResponseModel response = new ResponseModel();
            CodeService serviece = new CodeService();
            try
            {
                int? id = serviece.Add(model);

                response.Success = true;
                response.Message = "添加成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [MenuRoute("Index")]
        public JsonResult Edit(CodeInput model)
        {
            ResponseModel response = new ResponseModel();
            CodeService serviece = new CodeService();
            try
            {
                int id = serviece.Edit(model);
                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult UpdateField(int Id)
        {
            ResponseModel response = new ResponseModel();
            CodeService serviece = new CodeService();

            try
            {
                string field = Request.Params["field"];
                string value = Request.Params["value"];
                if (!string.IsNullOrWhiteSpace(field)
                    && !string.IsNullOrWhiteSpace(value))
                {
                    serviece.Update(Id, field, value);

                    response.Success = true;
                    response.Message = "修改成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "参数错误";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        //删除
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new CodeService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }


        #region 分类

        [MenuRoute("Index")]
        public JsonResult GetTreeNodes()
        {
            CodeTypeService service = new CodeTypeService();
            LayTableModel result = new LayTableModel();
            try
            {
                var list = service.GetTreeNodes();

                result.code = 1;
                result.data = list;
                result.count = 0;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }

            return Json(result);
        }

        [MenuRoute("TypeAdd")]
        public ActionResult AddOrEditType(int Id = 0)
        {
            int TypeParentId = string.IsNullOrEmpty(Request.Params["ParentId"])
                   ? 0 : int.Parse(Request.Params["ParentId"]);

            CodeTypeOutput model = null;
            CodeTypeService service = new CodeTypeService();
            int ParentId = 0;

            if (Id > 0)
            {
                model = service.Get(Id);
                ParentId = model.ParentId ?? 0;
            }
            else
            {
                model = new CodeTypeOutput();
                ParentId = TypeParentId;
            }


            IEnumerable<dynamic> CodeType_List = service.GetSelectListItem(true, ParentId);

            //新建一个SelectListItem的列表
            List<SelectListItem> CodeTypeselectItemList = new List<SelectListItem>();

            //自定义编辑
            foreach (var item in CodeType_List)
            {
                CodeTypeselectItemList.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.CodeName, Selected = (item.Id == TypeParentId) });
            }

            //数据传入视图
            ViewBag.TypeList = CodeTypeselectItemList;

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [MenuRoute("TypeAdd")]
        public JsonResult TypeAdd(CodeTypeInput model)
        {
            ResponseModel response = new ResponseModel();
            CodeTypeService serviece = new CodeTypeService();
            try
            {
                int? id = serviece.Add(model);
                if (id == -1)
                {
                    response.Success = false;
                    response.Message = "编码已经存在！";
                }
                else
                {
                    response.Success = true;
                    response.Message = "添加成功";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [MenuRoute("TypeEdit")]
        public JsonResult TypeEdit(CodeTypeInput model)
        {
            ResponseModel response = new ResponseModel();
            CodeTypeService serviece = new CodeTypeService();
            try
            {
                int result = serviece.Edit(model);

                if (result == -1)
                {
                    response.Success = false;
                    response.Message = "编码已经存在！";
                }
                else
                {
                    response.Success = true;
                    response.Message = "修改成功";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }


        [MenuRoute("TypeDelete")]
        public JsonResult TypeDeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new CodeTypeService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        #endregion

    }
}